package com.etc.service;

import com.etc.dao.MoviesDao;
import com.etc.entity.Categorys;
import com.etc.entity.Movies;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class MoviesServiceImpl implements MoviesService{

    @Autowired
    MoviesDao dao;

    /**
     * 通过电影名称模糊查询电影
     * */
    @Override
    public PageInfo<Movies> selectMovieByName(String movieName,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Movies> list = dao.selectMovieByName(movieName);
        PageInfo<Movies> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Movies> selectMovieByName2(String movieName,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Movies> list = dao.selectMovieByName2(movieName);
        PageInfo<Movies> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 通过电影编号no修改电影
     * */
    @Override
    @Transactional
    public boolean modifyMovie(String movieName, String movieContent, String movieImg, Double moviePrice,String movieDate, int movieTime, int movieNo) {
        int i=dao.modifyMovie(movieName,movieContent,movieImg,moviePrice,movieDate,movieTime,movieNo);
        return i>0;
    }

    @Override
    public boolean addMovie(String movieName, String movieContent, String movieImg, Double moviePrice, String movieDate, int movieTime, int movieStatus, int categoryNo, int typeNo) {
        int i = dao.addMovie(movieName,movieContent,movieImg,moviePrice,movieDate,movieTime,movieStatus,categoryNo,typeNo);
        return i>0;
    }


    /**
     * 通过电影编号no删除电影
     * */
    @Override
    @Transactional
    public boolean delMovieByNo(int movieNo) {
        int i=dao.delMovieByNo(movieNo);
        return i>0;
    }

    /**
     * @author luliang
     * 按上映时间排序选出首页电影的信息
     */
    @Override
    public List<Movies> getFirstImg(int startNo,int endNo) {
        return dao.queryIndexMovies(startNo,endNo);
    }

    @Override
    public Movies getMovById(int movieNo) {
        return dao.queryMovById(movieNo);
    }

    @Override
    public  PageInfo<Movies> getAllMoviesByName(String movieName) {
        PageHelper.startPage(1,5);
        List<Movies> moviesList = dao.queryAllMoviesByName(movieName);
        PageInfo<Movies> pageInfo = new PageInfo<Movies>(moviesList);
        return pageInfo;
    }

    @Override
    public boolean updateMovieStatus(int movieNo) {
        int i = dao.updateMovieStatus(movieNo);
        return i>0;
    }

    @Override
    public List<Categorys> getMovieCategorys() {
        return dao.queryMovieCategorys();
    }

    @Override
    public List<Movies> selectMovieBy() {
        return dao.selectMovieBy("");
    }

    @Override
    public List<Movies> getRandMovies() {
        return dao.queryRandMovies();
    }
}
